package com.lcf.SuperMarket;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @author 懒洋洋不懒
 * @version 1.0
 * @date 2025/4/11 21:24
 * @description
 */
public class UserInfoManager {


        private Set<UserInfo> users = new HashSet<>();
        private Scanner scanner = new Scanner(System.in);

        public void inital() {
            users.add(new UserInfo(1001, "冯程程", "铂金卡", 2000, "合法账户", "password1"));
            users.add(new UserInfo(1003, "孙悟空", "钻石卡", 5000, "被禁账户", "password2"));
            users.add(new UserInfo(1002, "张巧巧", "金卡", 500, "合法账户", "password3"));
        }

        public void mainMenu() {
            System.out.println("=====================================");
            System.out.println("欢迎使用超市用户管理系统");
            System.out.println("=====================================");
            System.out.println("1. 查询会员信息");
            System.out.println("2. 删除会员");
            System.out.println("3. 新增会员信息");
            System.out.println("4. 退出");
            System.out.println("=====================================");

            while (true) {
                System.out.print("请选择操作：");
                int choice = scanner.nextInt();
                scanner.nextLine(); // Consume newline

                switch (choice) {
                    case 1:
                        searchAll();
                        break;
                    case 2:
                        deleteUserInfo();
                        break;
                    case 3:
                        addUserInfo();
                        break;
                    case 4:
                        System.out.println("系统退出，谢谢您的使用！");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效选择，请重新输入！");
                }
            }
        }

        private void searchAll() {
            System.out.println("=====================================");
            System.out.println("查询会员信息");
            System.out.println("=====================================");
            for (UserInfo user : users) {
                System.out.println(user);
            }
        }

        private void deleteUserInfo() {
            System.out.println("=====================================");
            System.out.println("删除会员");
            System.out.println("=====================================");
            System.out.print("请输入要删除的会员ID号：");
            int id = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            boolean found = false;
            for (UserInfo user : users) {
                if (user.userId == id) {
                    found = true;
                    System.out.println("您确定要删除吗？(y/n)");
                    String confirm = scanner.nextLine();
                    if ("y".equalsIgnoreCase(confirm)) {
                        users.remove(user);
                        System.out.println("删除成功！");
                        return;
                    }
                }
            }
            if (!found) {
                System.out.println("对不起，您要删除的会员ID号不存在，请核实后再删除！");
            }
        }

        private void addUserInfo() {
            System.out.println("=====================================");
            System.out.println("新增会员信息");
            System.out.println("=====================================");
            System.out.print("请输入用户账号：");
            String account = scanner.nextLine();
            System.out.print("请选择卡类型: (金卡 铂金卡 钻石卡) ");
            String cardType = scanner.nextLine();
            System.out.print("请输入会员积分：");
            int points = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            System.out.print("请输入卡片状态：");
            String cardStatus = scanner.nextLine();
            System.out.print("请输入会员密码：");
            String password = scanner.nextLine();

            UserInfo newUser = new UserInfo(6000 + users.size() + 1, account, cardType, points, cardStatus, password);
            users.add(newUser);
            System.out.println("新增成功！");
        }
    }

